<?php

class indexControl extends commonControl {

    //版权信息|1
    function index() {
        $_SESSION['step'] = 1;
        $this->display();
    }

    //检测环境|2
    function check() {
        if ($_SESSION['step'] < 1) {
            go("index");
        }
        $_SESSION['step'] = 2;
        $uploadsize = ini_get("upload_max_filesize"); //允许上传的单个文件最大值
        $gd = gd_info(); //获得GD库所有信息
        $gdversion = $gd['GD Version']; //获得GD库版本
        $diskspace = get_size(disk_free_space('.'));
        $system = array(
            "phpversion" => PHP_VERSION,
            "phpos" => PHP_OS,
            "uploadsize" => $uploadsize,
            "gdversion" => $gdversion,
            "diskspace" => $diskspace
        );
        $this->assign("system", $system);
        $this->display();
    }

    //配置数据库连接|3
    function setconfig() {
        if ($_SESSION['step'] < 2) {
            go("check");
        }
        $_SESSION['step'] = 3;
        $this->display();
    }

    //安装数据库|4
    function installdb() {
        if ($_SESSION['step'] < 3) {
            go("setconfig");
        }
        $_SESSION['step'] = 4;
        $this->writeconfig();
        $this->display();
    }

    //写入数据库配置
    function writeconfig() {
        $str = '<?php
                if (!defined("PATH_HD"))exit;
                return ';
        $str.=var_export($_POST['db'], true) . ';?>';
        $dbconfigfile = "../config/database.php";
        file_put_contents($dbconfigfile, $str);
    }

    //写入数据库
    function createtable() {
        if ($_SESSION['step'] < 3) {
            go("setconifg");
        }
        $_SESSION['step'] = 4;
        $dbconfig = include "../config/database.php";
        $dbprefix = $dbconfig['DB_PREFIX']; //表前缀
        $sql = file_get_contents(PATH_APP . '/data/hdcms.sql');
        $preg = "/(CREATE|USE).*;/sU";
        preg_match_all($preg, $sql, $tables);
        //连接数据库
        $db = mysql_connect($dbconfig['DB_HOST'], $dbconfig['DB_USER'], $dbconfig['DB_PASSWORD']);
        mysql_query("CREATE DATABASE " . $dbconfig['DB_DATABASE'] . " CHARSET utf8");
        mysql_query("USE " . $dbconfig['DB_DATABASE']);
        if (!$db) {
            $this->error("数据库连接配置错误");
        }
        $buffer = ini_get("output_buffering");
        $error = 0; //错误状态码
        foreach ($tables[0] as $t) {
            $preg = "/CREATE\s+TABLE\s+IF\s+NOT\s+EXISTS\s+`(.*?)`/iU";
            preg_match($preg, $t, $tb);
            echo str_repeat(" ", $buffer); //将缓冲区填满
            if ($tb) {
                $t = str_replace($tb[1], $dbprefix . $tb[1], $t);
                if (mysql_query($t)) {
                    echo "<span style='font-size:14px;height:20px;display:block;width:500px;'>数据表" . $dbprefix . $tb[1] . "创建成功.....</span>";
                } else {
                    $error = 1;
                    echo "<span style='font-size:14px;height:20px;display:block;width:500px;color:#f00;'>数据表" . $dbprefix . $tb[1] . "创建失败.....</span>";
                }
//                sleep(1);
            } else {
                mysql_query($t);
            }
            ob_flush();
            flush();
        }
        if (!$error) {
            echo "<h3 style='color:#7F9CCC;font-size;22px;'>数据表创建完成</h3>";
        }
        echo "<script>
            setTimeout(function(){
                    parent.window.location.href='" . __CONTROL__ . "/finish';
            },2000)
            
                </script>";
        exit;
    }

    //安装完成
    function finish() {
        if ($_SESSION['step'] < 4) {
            go("index");
        }
        unset($_SESSION['step']); //卸载安装步骤
        file_put_contents(PATH_APP . "/lock.php", "");
        @unlink(PATH_ROOT . "/index.html");
        $this->display();
    }

}